<?php
/**
 * Created by PhpStorm.
 * User: kaycn
 * Date: 18-9-18
 * Time: 下午5:17
 */
namespace Pay;

class log{

    public $file = "./log/new.log";

    public static $data;

    public  $json = "";

    public function deeldata(){

        self::$data = preg_replace('/\\\n/',"",self::$data);


        if(preg_match('/\[/',self::$data)){

            self::$data = json_decode(self::$data,true);
            $tmp = [];
            foreach (self::$data as $key=>$val){

                $val['goods'] = substr($val['goods'],0,strlen($val["goods"])-15);

                $tmp[$key] = $val;


            }
            self::$data = $tmp;

            $this->json = json_encode(self::$data);


//
//            self::$data = preg_replace('/\[/',"",self::$data);
//
//            self::$data = preg_replace('/\]/',"",self::$data);

           // preg_match_all("/\{[^{]+\}/i",self::$data,self::$data);

          //  self::$data = self::$data[0];

            //规范json构造

//            foreach (self::$data[0] as $key => $val){
//
//                $this->json = $this->json.$key.":".json_encode($val);
//
//            }
//            $this->json = '{'.$this->json.'}';

        }
        else{

            $this->json = self::$data;

            echo 2;

            self::$data = json_decode(self::$data,true);

        }




    }

    public function __construct($data)
    {
        self::$data = $data;
        //处理数据
        $this->deeldata();


    }

    public function save(){
        (new memcache)->sava(self::$data);

        if(file_exists($this->file)){

            rename($this->file,"./log/".date("Y-m-d H:i:s",time()).".log");

        }
        else{

            $fh = fopen($this->file,"w");

            fclose($fh);

        }
        $this -> put();
    }

    public function put(){


        file_put_contents($this->file,date("Y-m-d H:i:s",time())."\n".$this->json);


    }


}